package cn.itcast.wushangxiantu.Fragment.guozhan;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.google.gson.Gson;

import java.io.IOException;

import cn.itcast.wushangxiantu.BaseAdapter.GuoJiaChengYuanAdapter;
import cn.itcast.wushangxiantu.UserData.GuoJiaUser;
import cn.itcast.wushangxiantu.Activity.GuoZhanActivity;
import cn.itcast.wushangxiantu.Activity.Login;
import cn.itcast.wushangxiantu.OkhttpUtil;
import cn.itcast.wushangxiantu.R;
import cn.itcast.wushangxiantu.ToastUtil;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link GuoJiaChengYuanFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class GuoJiaChengYuanFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private Context context;
    private int gj_id;
    private View view;
    private ViewHolder viewHolder;
    private GuoJiaChengYuanAdapter adapter;

    public GuoJiaChengYuanFragment(Context context, int gj_id) {
        this.context = context;
        this.gj_id = gj_id;
    }

    public GuoJiaChengYuanFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment GuoJiaChengYuanFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static GuoJiaChengYuanFragment newInstance(String param1, String param2) {
        GuoJiaChengYuanFragment fragment = new GuoJiaChengYuanFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_guo_jia_cheng_yuan, container, false);
        viewHolder=new ViewHolder(view);
        return view;
    }

    class ViewHolder {
        public View rootView;
        public ListView lv;
        public Button btnShuXin;
        public TextView tvNumber;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.lv = (ListView) rootView.findViewById(R.id.lv);
            this.tvNumber = (TextView) rootView.findViewById(R.id.tvNumber);
            this.btnShuXin = (Button) rootView.findViewById(R.id.btnShuaXin);
            btnShuXin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    getGuoJiaUser(Login.getUser().getUsernumber(),Login.getUser().getPassword(),gj_id);
                }
            });
            getGuoJiaUser(Login.getUser().getUsernumber(),Login.getUser().getPassword(),gj_id);
        }

        private void getGuoJiaUser(String usernumber, String password, final int gj_id) {
            OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "&need=getGuoJiaUser" + "&userNumber=" + usernumber + "&passWord=" + password + "&gj_id=" + gj_id, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    try {
                        Log.v("getGuoJiaUser",e.getMessage());
                    }catch (Exception e1){

                    }
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String res=response.body().string();
                    Log.v("getGuoJiaUser",res);
                    Message message=new Message();
                    GuoJiaUser guoJiaUser=new Gson().fromJson(res,GuoJiaUser.class);
                    message.obj=guoJiaUser;
                    message.what=1;
                    handler.sendMessage(message);
                }
            });
        }
        Handler handler=new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message message) {
                switch (message.what){
                    case 1:
                        GuoJiaUser user= (GuoJiaUser) message.obj;
                        if (null!=user){
                            if (null==user.getGuoJiaUser()){
                                ToastUtil.toast(context,user.getStw());
                            }else {
                                if (null==adapter){
                                    adapter=new GuoJiaChengYuanAdapter(context);
                                }
                                adapter.setGuoJiaUser(user);
                                lv.setAdapter(adapter);
                                tvNumber.setText("人数"+user.getGuoJiaUser().size()+"/"+ (GuoZhanActivity.getGuoJiaXinXi().getDj()/5*2));
                            }
                        }else {
                            ToastUtil.toast(context,"服务器炸了");
                        }
                        break;
                }
                return false;
            }
        });
    }
}